<?php
namespace app\common\component\notice;

class NoticeService {
	public function getNotices($page = 10, $simple = false) {
		return (new NoticeModel()) -> order('notice_id DESC') -> paginate($page, $simple);	
	}
	
	public function getNotice($notice_id) {
		if($notice_id <= 0) {
			$getNotice = NoticeModel::order('notice_id DESC') -> find();
		} else {
			$getNotice = NoticeModel::get($notice_id);
		}

		if($getNotice) {
			return [
				'code' => 0,
				'message' => 'Success',
				'data' => [
					'notice' => $getNotice
				]
			];
		}
		return [
			'code' => -1,
			'message' => 'Fail'
		];
	}

	public function create($data) {
    	$noticeModel = new NoticeModel($data);

		// $noticeModel -> allowField(true) -> save();
    	if($noticeModel -> save()) {
    		return [
    			'code' => 0,
    			'message' => '公告创建成功',
    			'data' => [
    				'notice' => $noticeModel
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '公告创建失败'
		];
    }

	public function update($data, $notice_id) {
		$noticeModel = new NoticeModel();

		$updateNotice = $noticeModel -> where('notice_id', $notice_id) -> update($data);

    	if($updateNotice) {
    		return [
    			'code' => 0,
    			'message' => '公告修改成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '公告修改失败'
		];
	}

	public function delete($notice_id) {
		$getNotice = NoticeModel::get($notice_id);

    	if($getNotice -> delete()) {
    		return [
    			'code' => 0,
    			'message' => '公告删除成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '公告删除失败'
		];
	}
}
?>